package com.google.unizone.server;

import java.util.LinkedList;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.jdo.annotations.Embedded;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;  
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable
public class TestStudet{
	/*
	 * courseID will be in the format of:
	 * uniID_yyyy/(A|B)
	 */
	//@PrimaryKey
	//@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	//private Key id;
	@PrimaryKey
	@Persistent
	private String courseID;
	@Persistent
	private String courseName;
	
	@Persistent(defaultFetchGroup="true")
	//@Embedded
	private List<Lesson> lessonTimes;
	
	
	//@Persistent
	//private List<String> lessonKeys;
	
	
	
	@Persistent
	private int day1, month1, year1;
	@Persistent
	private int day2, month2, year2;
	
	@Persistent(defaultFetchGroup="true")
	@Embedded
	private MyTime hour1, hour2;	
	
	@Persistent
	private String semester;
	
	@Persistent
	private Set<String> studentIDList;
	
	//TODO add lesson Times parsing
	public TestStudet(String courseID, String courseName, String professorName, List<Lesson> lessonTimes, MyDate moedADate, MyDate moedBDate, String semester, Set<String> studentIDList){
		this.courseID = courseID;
		this.courseName = courseName;
		this.lessonTimes = new LinkedList<Lesson>(lessonTimes);
		
		this.day1=moedADate.getDay();
		this.month1=moedADate.getMonth();
		this.year1=moedADate.getYear();
		this.hour1=new MyTime(moedBDate.getHour().getHours(),moedBDate.getHour().getMinutes());
		
		this.day2=moedBDate.getDay();
		this.month2=moedBDate.getMonth();
		this.year2=moedBDate.getYear();
		this.hour2=moedBDate.getHour();
		
		this.semester = semester;
		this.studentIDList = new TreeSet<String>(studentIDList);
		/*this.lessonKeys=new LinkedList<String>();
		for (Lesson l:lessonTimes){
			this.lessonKeys.add(l.getKey());
		}*/
	}
	
	public Set<String> getStudentIDList() {
		return studentIDList;
	}


	public void setStudentIDList(Set<String> studentIDList) {
		this.studentIDList = studentIDList;
	}
	
	public String getID() {
		return this.courseID;
	}
	public String getCourseName() {
		return this.courseName;
	}
	public List<Lesson> getLessonTimes() {
		return this.lessonTimes;
	}
	public MyDate getMoedADate() {
		return new MyDate(this.day1, this.month1, this.year1, this.hour1);
	}
	
	public MyDate getMoedBDate() {
		return new MyDate(this.day2, this.month2, this.year2, this.hour2);
	}
	
	public String getSemester() {
		return this.semester;
	}
	public Set<String> getStudents() {
		return this.studentIDList;
	}
	
	//SETTERS
	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}
	
	public void setLessonTimes(List<Lesson> lessonTimes) {
		this.lessonTimes = lessonTimes;
	}
	
	public void setID(String courseID) {
		this.courseID  = courseID; 
	}
	public void setSemester(String semester) {
		this.semester = semester;
	}
	
	public void setMoedADate(MyDate date) {
		this.day1=date.getDay();
		this.month1=date.getMonth();
		this.year1=date.getYear();
		this.hour1=date.getHour();
	}
	
	public void setMoedBDate(MyDate date) {
		this.day2=date.getDay();
		this.month2=date.getMonth();
		this.year2=date.getYear();
		this.hour2=date.getHour();
	}
	
	public void addStudent(String id) {
		if (!isStudentInCourse(id)) {
			this.studentIDList.add(id);
		}
	}
	public void removeStudent(String id) {
		if (isStudentInCourse(id)) {
			this.studentIDList.remove(id);
		}
	}
	public void addStudent(Student stu) {
		if (!isStudentInCourse(stu)) {
			this.studentIDList.add(stu.getFacebookID());
		}
	}
	public void removeStudent(Student stu) {
		if (!isStudentInCourse(stu)) {
			this.studentIDList.remove(stu.getFacebookID());
		}
	}
	public boolean isStudentInCourse(String studentID) {
		return this.studentIDList.contains(studentID);
	}
	public boolean isStudentInCourse(Student student) {
		return isStudentInCourse(student.getFacebookID());
	}
	
	public String nameToString (){
		return this.getCourseName() +  " " +  "(" + this.getID() + ")"; 
	}
	
	
	public String toString(boolean isHeader)
	{
		String courseName;
		courseName =  this.nameToString();
		if (isHeader) {
			return "<h2 id=\"course_toString\">" + courseName + "</h2>";
		}
		else {
			if (courseName.length() >=70)
				return  "<a href = \"CourseGroupPage.jsp?courseID=" + this.getID() + "\" id=\"course_toString\"><img src =\"untitled2.bmp\"/></a> <a rel=\"htmltooltip\" >"  + courseName.substring(0,70) + "</a>"
				+ "<div class=\"htmltooltip\">" + courseName + "</div>";
			else
				return  "<a href = \"CourseGroupPage.jsp?courseID=" + this.getID() + "\" id=\"course_toString\"><img src =\"untitled2.bmp\"/></a> <a rel=\"htmltooltip\" >"  + courseName + "</a>"
				+ "<div class=\"htmltooltip\">" + courseName + "</div>";
			
		}
			
	}
}
